Elon Musk has appeared in court to testify in a defamation lawsuit brought by a British cave diver who he called a “pedo guy” on Twitter.

Giving evidence in Los Angeles, the Tesla founder insisted his slur was not meant to be taken literally and was only a “taunt” aimed at Vernon Unsworth during a war of words over his offer to help rescue a schoolboy football team from a flooded cave in Thailand.

Mr Unsworth has accused the billionaire inventor of falsely labelling him a paedophile and is seeking unspecified damages.

The case stems from Musk offering a “kid-sized” submarine to rescuers working to free 12 Thai schoolboys and their coach from the waterlogged Tham Luang Nang Non cave complex in Chiang Rai province in July 2018.

Rescuers – including Mr Unsworth – led the football team to safety without using the 6ft vessel, which the British diver later said “had absolutely no chance of working”.

“He can stick his submarine where it hurts,” he told CNN during an interview, dismissing Musk’s offer to help as a “PR stunt”.

Thailand cave rescue operation: in pictures Show all 100 1 /100 Thailand cave rescue operation: in pictures Thailand cave rescue operation: in pictures The 12 boys and their coach in the cave REUTERS Thailand cave rescue operation: in pictures A rescue team stands in the entrance to the cave AP Thailand cave rescue operation: in pictures A relative shows a picture of the boys after they were found on 2 July AFP/Getty Thailand cave rescue operation: in pictures Thai military inside the cave complex EPA Thailand cave rescue operation: in pictures The 12 boys and their soccer coach who were rescued from a flooded cave arrive for a news conference in the northern province of Chiang Rai, Thailand, REUTERS Thailand cave rescue operation: in pictures The boys and their soccer coach rescued from a flooded cave play soccer REUTERS Thailand cave rescue operation: in pictures The young footballers and their coach appeared healthy when they appeared before the media for the first time since the rescue AFP/Getty Images Thailand cave rescue operation: in pictures epa06896322 Some of the 12 members of the Wild Boar soccer team, who were rescued from the Tham Luang cave, greet the media during a military governmental TV pool broadcasting program at Chiang Rai Provincial Administrative Organization in Chiang Rai province, Thailand, 18 July 2018. The thirteen members of Wild Boar child soccer team, including their assistant coach, who were trapped in the Tham Luang cave since 23 June 2018, made their first appearance on the day for a tightly-controlled interview with the media after they were rescued, before returning to their homes with families. EPA/PONGMANAT TASIRI PONGMANAT TASIRI EPA Thailand cave rescue operation: in pictures Coach Ekapol Chantawong REUTERS Thailand cave rescue operation: in pictures The 12 rescued members of the Wild Boar soccer team are greeted by their classmates on a mini soccer pitch EPA Thailand cave rescue operation: in pictures Rescued Thai football coach Ekkapol Chantawong together with 12 children pays tribute to Thai navy diver, Samarn Kunan, who died whilst attempting to rescue the boys AFP/Getty Images Thailand cave rescue operation: in pictures Twelve young footballers and their coach rescued from a Thai caves being treated in hospital EPA Thailand cave rescue operation: in pictures Rescued soccer team members posing with a sketch of the Thai Navy SEAL diver who died while trying to rescue them AP Thailand cave rescue operation: in pictures Players writing messages on a drawing of former Navy SEAL diver Saman Kunan who died during attempts to rescue them AFP/Getty Images Thailand cave rescue operation: in pictures Rescued "Wild Boars" football team members (top row L to R) Adul Sam-on, 14, Ekkarat Wongsukchan, 14, Phipat Photi, 15, Chanin Wiboonrungrueang, 11, (middle row L to R) Pornchai Khamluang, 16, Duangphet Promthep, 13, Peerapat Sompiengjai, 16, Sompong Jaiwong, 13, (bottom row L to R) Mongkol Boonpiem, 13, Prachak Sutham, 14, Nutthawut Thakamsong, 14, and Phanumas Saengdee, 13, recovering at the hospital in Chiang Rai province AFP/Getty Images Thailand cave rescue operation: in pictures This handout video grab taken from footage released by The Thai government public relations department (PRD) and Government spokesman bureau on July 11, 2018 shows members of the "Wild Boars" football team being treated at a hospital in Chiang Rai. The 12 boys rescued from a Thai cave were passed "sleeping" on stretchers through the treacherous passageways, a former Thai Navy SEAL told AFP on July 11, giving the first clear details of an astonishing rescue mission that has captivated the world. / AFP PHOTO / Thai government public relations department (PRD) AND Government spokesman bureau / Handout / RESTRICTED TO EDITORIAL USE - MANDATORY CREDIT "AFP PHOTO / The government public relations department (PRD) and Government spokesman bureau " - NO MARKETING NO ADVERTISING CAMPAIGNS - DISTRIBUTED AS A SERVICE TO CLIENTSHANDOUT/AFP/Getty Images HANDOUT AFP/Getty Images Thailand cave rescue operation: in pictures The life vest belonging to Duangpetch Promthep, the rescued captain of the "Wild Boars", REUTERS Thailand cave rescue operation: in pictures Divers from the rescue mission, from left to right: Chris Jewell, Mike Clayton, Rick Stanton, Lance-corporal Connor Rae, Josh Bratchley, Gary Mitchell and Jim Way who helped to save 12 schoolboys and their football coach from a flooded cave in Thailand, speak to media after arriving back at London's Heathrow Airport PA Thailand cave rescue operation: in pictures Volunteers gather for a ceremony before the clean up operation at the Tham Luang cave AFP/Getty Images Thailand cave rescue operation: in pictures The last four Thai Navy SEALs come out safely after completing the rescue mission inside a cave where 12 boys and their soccer coach have been trapped since June 23, in Mae Sai, Chiang Rai province, northern Thailand. Thailand's navy SEALs say all 12 boys and their soccer coach have been rescued from a flooded cave in far northern Thailand, ending an ordeal that lasted more than two weeks. Royal Thai Navy/AP Thailand cave rescue operation: in pictures People celebrate AP Thailand cave rescue operation: in pictures A woman lifts her child near the hospital, seen at left, as ambulance transporting members of the children's football team arrives AFP/Getty Images Thailand cave rescue operation: in pictures Volunteers react after hearing all boys were rescued REUTERS Thailand cave rescue operation: in pictures People attend a funeral of Samarn Kunan, a former Thai navy diver who died working to rescue a young soccer team trapped for days in a flooded cave, at a temple in Roi Et province, Thailand, July 14, 2018. REUTERS/Stringer NO RESALES. NO ARCHIVE STRINGER REUTERS Thailand cave rescue operation: in pictures CHIANG RAI, THAILAND - July 10: Onlookers at the junction in front of Chiangrai Prachanukroh Hospital watch and cheer as ambulances transport the last rescued schoolboys & their coach from a helipad nearby to Chiangrai Prachanukroh Hospital on July 10, 2018 in Chiang Rai, Thailand. Divers began an effort to pull the 12 boys and their soccer coach on Sunday morning after they were found alive in the cave at northern Thailand. Videos released by the Thai Navy SEAL shows the boys, aged 11 to 16, and their 25-year-old coach are in good health in Tham Luang Nang Non cave and the challenge now will be to extract the party safely. (Photo by Linh Pham/Getty Images) Linh Pham Getty Images Thailand cave rescue operation: in pictures Rescue personnel work at the Tham Luang cave REUTERS Thailand cave rescue operation: in pictures Rescue personnel work at the Tham Luang cave REUTERS Thailand cave rescue operation: in pictures Rescue personnel work at the Tham Luang cave complex in the northern province of Chiang Rai, Thailand July, 2018 in this still image taken from a video obtained from social media. THAI NAVY SEAL/via REUTERS THIS IMAGE HAS BEEN SUPPLIED BY A THIRD PARTY. MANDATORY CREDIT. NO RESALES. NO ARCHIVES. SOCIAL MEDIA REUTERS Thailand cave rescue operation: in pictures Onlookers at the junction in front of Chiangrai Prachanukroh Hospital watch and cheer Getty Images Thailand cave rescue operation: in pictures Journalists celebrate after a news conferenc REUTERS Thailand cave rescue operation: in pictures An ambulance, reportedly carrying members of the rescued football team, arrives at a hospital AFP/Getty Images Thailand cave rescue operation: in pictures Thai Navy divers in the Tham Luang cave Thai Navy SEAL via Getty Images Thailand cave rescue operation: in pictures Rescue personnel work at the Tham Luang cave REUTERS Thailand cave rescue operation: in pictures Thai media listen Chiang Rai province acting Gov. Narongsak Osatanakorn AP Thailand cave rescue operation: in pictures Police and military personnel use umbrellas to cover a stretcher near a helicopter and an ambulance at a military airport in Chiang Rai as rescue operations continue for those still trapped inside the cave in Khun Nam Nang Non Forest Park in the Mae Sai district. Four boys among the group of 13 trapped in a flooded Thai cave for more than a fortnight were rescued on July 8 after surviving a treacherous escape, raising hopes elite divers would also save the others soon AFP/Getty Thailand cave rescue operation: in pictures Thai military medical personnel walking inside a restricted area as preparations are made to transport rescued boys from Tham Luang cave to a hospital EPA Thailand cave rescue operation: in pictures Thai authorities placing a rescued boy in an ambulance EPA/Chiang Rai Thailand cave rescue operation: in pictures Paramedics, believed to be carrying a boy who has been evacuated after being trapped inside a flooded cave, approach a helicopter in Chiang Rai Chiang Rai Tourist Police via Reuters Thailand cave rescue operation: in pictures This frame grab from video tweeted by Elon Musk shows a "tiny kid-sized submarine" being tested in a pool at Palisades Charter High School, in California. Musk's Space X rocket company tested the submarine that could potentially help rescue the remaining children and their coach trapped in a flooded cave in Thailand. Elon Musk via AP Thailand cave rescue operation: in pictures Thai military personnel preparing to move the boys to a hospital EPA Thailand cave rescue operation: in pictures Emergency workers carrying one of the boys rescued from the cave EPA/Chiang Rai Thailand cave rescue operation: in pictures A Thai nurse walks past an ambulance at a covered area of the Chiangrai Prachanukroh Hospital EPA Thailand cave rescue operation: in pictures Indian schoolchildren hold placards and pictures during a prayer event for the safe rescue of young football players and their coach AFP/Getty Images Thailand cave rescue operation: in pictures An ambulance exits from the Tham Luang cave area AFP/Getty Images Thailand cave rescue operation: in pictures A military helicopter carrying rescued schoolboys approaches to land at a military airport in Chiang Rai. REUTERS Thailand cave rescue operation: in pictures A group of volunteers prepare to leave in search of alternative entry points to the Tham Luang cave area AFP/Getty Thailand cave rescue operation: in pictures Thai soldiers drag a water pipe to bypass water from the mountain and to not get back inside the cave AP Thailand cave rescue operation: in pictures Hundreds of rescuers with equipment continue the rescue operation Getty Thailand cave rescue operation: in pictures Monks lead out family members of the boys from the cave site after a morning prayer Getty Thailand cave rescue operation: in pictures Thai rescue workers take a break AFP/Getty Thailand cave rescue operation: in pictures Water pumps push water out of the cave Getty Thailand cave rescue operation: in pictures Relatives, of the 12 schoolboys and their soccer coach trapped inside a flooded cave in the Tham Luang cave complex cook, for rescue workers and volunteers near the cave complex REUTERS Thailand cave rescue operation: in pictures Rescue teams carry water pipes to the entrance of cave complex AP Thailand cave rescue operation: in pictures Thai soldiers and police officers are seen as rescue operations continue for the 12 boys and their football team coach trapped in Tham Luang cave at Khun Nam Nang Non Forest Park in the Mae Sai district of Chiang Rai province on July 7, 2018. More than 100 chimneys are being drilled into the mountainside in a frantic bid to reach a Thai youth football team trapped in a cave complex below, the head of the rescue mission said on July 7 AFP/Getty Thailand cave rescue operation: in pictures Family members pray in front of a Buddhist statue near a cave where 12 boys and their soccer coach have been trapped since June 23, in Mae Sai, Chiang Rai province, in northern Thailand Saturday, July 7, 2018. The soccer coach trapped in a cave with 12 Thai boys apologized to their parents in the first letter he and the team have sent out through divers, in which the boys say they're doing well and missing their families AP Thailand cave rescue operation: in pictures Thai military inside the cave complex EPA Thailand cave rescue operation: in pictures Australian Federal Police and Defense Force personnel talk with a Thai diver AP Thailand cave rescue operation: in pictures A rescuer prepares a light for diving to reach the 12 boys and their soccer coach AP Thailand cave rescue operation: in pictures Soliders are seen near the Tham Luang cave complex, where 12 schoolboys and their soccer coach are trapped inside a flooded cave, in the northern province of Chiang Rai, Thailand REUTERS Thailand cave rescue operation: in pictures Water is pumped from a flooded cave AP Thailand cave rescue operation: in pictures Thai rescuers rest during the ongoing rescue operation EPA Thailand cave rescue operation: in pictures Thai military personnel inside a cave complex EPA Thailand cave rescue operation: in pictures Rescue teams assemble at the entrance to the Tham Luang cave complex AP Thailand cave rescue operation: in pictures Thai Navy soldiers in the flooded Tham Luang cave during rescue operations AFP/Getty Thailand cave rescue operation: in pictures Local Thai National Park Rescuers relax at base camp Getty Images Thailand cave rescue operation: in pictures Journalists work near Tham Luang cave complex REUTERS Thailand cave rescue operation: in pictures A Thai policeman guards an area under rainfall AFP/Getty Images Thailand cave rescue operation: in pictures A Thai policeman guards an area under rainfall near the Tham Luang cave AFP/Getty Images Thailand cave rescue operation: in pictures ***BESTPIX*** CHIANG RAI, THAILAND - JULY 7: Family members of the boys trapped in the cave are questioned by the media on July 7, 2018 in Chiang Rai, Thailand. The 12 boys and their soccer coach have been found alive in the cave where they've been missing for over a week after monsoon rains blocked the main entrance in northern Thailand. Videos released by the Thai Navy SEAL shows the boys, aged 11 to 16, and their 25-year-old coach are in good health in Tham Luang Nang Non cave and the challenge now will be to extract the party safely. (Photo by Lauren DeCicca/Getty Images) Lauren DeCicca Thailand cave rescue operation: in pictures Divers gather before they enter to the Tham Luang cave, where 12 boys and their soccer coach are trapped, in the northern province of Chiang Rai, Thailand REUTERS Thailand cave rescue operation: in pictures Thai military bring water pumps to the cave. Videos released by the Thai Navy SEAL shows that the boys, aged 11 to 16, and their 25-year-old coach are in good health in Tham Luang Nang Non cave and the challenge now will be to extract the party safely. Getty Images Thailand cave rescue operation: in pictures An honour guard holds up a picture of Samarn Kunan, 38, a former member of Thailand's elite navy SEAL unit who died working to save 12 boys and their soccer coach trapped inside a flooded cave, at an airport in Rayong province Reuters Thailand cave rescue operation: in pictures Military personnel are seen in front of the Tham Luang cave REUTERS Thailand cave rescue operation: in pictures The former navy SEAL diver, Saman Kunan, who died trying to help save the trapped boys Saman Kunan/Facebook Thailand cave rescue operation: in pictures Rescue workers convene behind a large pile of oxygen tanks AFP/Getty Images Thailand cave rescue operation: in pictures A Buddhist monk leads military honor guards carrying a flag-draped coffin containing former Thai Navy Seal Petty Officer 1st class Saman Kunan, who died in the ongoing Tham Luang cave rescue operations, during a repatriation ceremony at Mae Fah Luang international airport. EPA Thailand cave rescue operation: in pictures Thai students pray for the ongoing rescue operations EPA Thailand cave rescue operation: in pictures Thai soldiers and police officers pay their respects as a Thai Navy plane carrying the body of Saman Kunan takes off AFP/Getty Images Thailand cave rescue operation: in pictures Rescue workers prepare frogsuits for soldiers AFP/Getty Images Thailand cave rescue operation: in pictures The diver's death raises serious doubts over the safety of attempting to bring the 12 boys and their football coach out through the cramped, waterlogged passageways of the Tham Luang cave. AP Thailand cave rescue operation: in pictures Family members pray before a shrine in Tham Luang cave area AFP/Getty Images Thailand cave rescue operation: in pictures A family member prays before a shrine in Tham Luang cave area AFP/Getty Images Thailand cave rescue operation: in pictures Thai rescue teams use headlamps to enter a pitch-black cave complex where 12 boys and their soccer coach went missing, in Mae Sai, Chiang Rai province, northern Thailand, Monday, July 2, 2018. The group was discovered late July 2 after 10 days totally cut off from the outside world, and while they are for the most physically healthy, experts say the ordeal has likely taken a mental toll that could worsen the longer the situation lasts. AP Thailand cave rescue operation: in pictures The mother of a trapped child reacts during the ongoing rescue operations EPA Thailand cave rescue operation: in pictures Family members pray before a shrine in Tham Luang cave area AFP/Getty Images Thailand cave rescue operation: in pictures Grandmother Kham-aoey Promthep, 64, shows a photograph of her grandson, Duangpetch Promthep, who is one of 13 members EPA Thailand cave rescue operation: in pictures A Thai student shows an image of her classmate Panumas Sangdee, who is one of 13 members of a soccer team trapped EPA Thailand cave rescue operation: in pictures Buddhist monk Kruba Boonchum visits the site to perform religious rituals Getty Images Thailand cave rescue operation: in pictures Thai Prime Minister Prayut Chan-o-cha visits the site REUTERS Thailand cave rescue operation: in pictures British cave-divers (from left to right) Richard William Stanton, Robert Charles Harper and John Volanthen join the rescue effort AFP/Getty Images Thailand cave rescue operation: in pictures Divers prepare for the rescue mission EPA Thailand cave rescue operation: in pictures A rescuer enters the cave AP Thailand cave rescue operation: in pictures The father of one of the missing boys thanks soldiers after the boys are found AFP/Getty Images Thailand cave rescue operation: in pictures Relatives celebrate after they hear that the boys have been found AFP/Getty Images Thailand cave rescue operation: in pictures Relatives pray after they hear that the boys have been found Getty Images Thailand cave rescue operation: in pictures Relatives celebrate after they hear that the boys have been found AFP/Getty Images Thailand cave rescue operation: in pictures General Bancha Duriyaphan announces the news to the press Getty Images Thailand cave rescue operation: in pictures With the rescue mission still ongoing, Thai soldiers carry equipment into the cave EPA Thailand cave rescue operation: in pictures With the rescue mission still ongoing, Thai soldiers carry equipment into the cave EPA Thailand cave rescue operation: in pictures Water is being pumped out of the flooded cave complex in an effort to make the rescue easier Getty Images

Two days later, the tech entrepreneur lashed out at Mr Unsworth in a series of tweets, calling him “sus[picious]” and a “pedo guy”.

Testifying to a packed courtroom on Tuesday, Musk conceded his tweets were “not classy” but said they were written in response to “an unprovoked attack on what was a good-natured attempt to help the kids”.

“It was wrong and insulting, and so I insulted him back,” the SpaceX founder told jurors.

Under questioning from Mr Unsworth’s lawyer, Lin Wood, Musk insisted his slur did not amount to a genuine accusation of paedophilia.

“I assume he did not mean to physically sodomise me with a submarine … just as I didn’t literally mean he was a paedophile,” he said.

“I apologised in a tweet and again in the deposition, and I’ll say it again – I apologise to Mr Unsworth,” added Musk, looking directly at the diver, who sat stone-faced throughout the testimony.

Musk has claimed "pedo guy" is a common insult in his native South Africa and was taken to mean "creepy old man" – a defence he first offered in September.

Asked why he had not made it clear earlier that he was not calling Mr Unsworth a paedophile, he told the court: "I think that would be worse. It would seem disingenuous as an apology."

If he had called someone a "motherf**ker it would seem "sarcastic" to say "I did not mean they committed incest," he suggested.​

But Taylor Wilson, another member of Mr Unsworth’s legal team, said Musk's tweet was more than a slip-up and had falsely branded the dive a predator to millions of people “in what should have been one of the proudest moments of his life”.

Support free-thinking journalism and attend Independent events

Musk is expected to return to the witness stand on Wednesday for more questioning by his own lawyer, Alex Spiro, who told the court Mr Unsworth deserved no compensation for “joking, taunting tweets in a fight between men”.

The shame inflicted on the diver by Musk’s slur had been mitigated by attention he received following the rescue, Mr Spiro claimed, as well as honours from the Thai king and British prime minister.

US district judge Stephen Wilson has explained the case hinges on whether a reasonable person would take Musk’s tweets to mean that he was calling Mr Unsworth a paedophile.

While Musk deleted the offending July 2018 tweet and later apologised, he also doubled down on the accusation during an exchange with a Buzzfeed reporter seeking a comment on the threat of legal action.

“Stop defending child rapists,” he wrote in an August 2018 email to the journalist.